<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Goods\Service;

use Doctrine\ORM\EntityManager;
use Goods\Entity\GoodsOtherTagGoods;

class GoodsOtherTagGoodsManager
{
    private $entityManager;

    public function __construct(
        EntityManager $entityManager
    )
    {
        $this->entityManager = $entityManager;
    }

    /**
     * 添加聚合标签商品
     * @param array $data
     * @return GoodsOtherTagGoods
     * @throws \Doctrine\ORM\Exception\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function addOtherTagGoods(array $data)
    {
        $otherTagGoods = new GoodsOtherTagGoods();
        $otherTagGoods->valuesSet($data);

        $this->entityManager->persist($otherTagGoods);
        $this->entityManager->flush();

        return $otherTagGoods;
    }

    /**
     * 编辑聚合标签商品
     * @param array $data
     * @param GoodsOtherTagGoods $goodsOtherTagGoods
     * @return GoodsOtherTagGoods
     * @throws \Doctrine\ORM\Exception\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function editOtherTagGoods(array $data, GoodsOtherTagGoods $goodsOtherTagGoods)
    {
        $goodsOtherTagGoods->valuesSet($data);
        $this->entityManager->flush();

        return $goodsOtherTagGoods;
    }

    /**
     * 更新标签商品排序
     * @param array $data
     * @return void
     * @throws \Doctrine\ORM\Exception\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     * @throws \Doctrine\Persistence\Mapping\MappingException
     */
    public function updateOtherTagGoodsSort(array $data)
    {
        if (!empty($data['tagGoodsSort'])) foreach ($data['tagGoodsSort'] as $otherTagGoodsId => $sortValue) {
            $otherTagGoods = $this->entityManager->getRepository(GoodsOtherTagGoods::class)->findOneBy(['otherTagGoodsId' => $otherTagGoodsId]);
            if ($otherTagGoods) {
                $otherTagGoods->setOtherTagGoodsSort($sortValue);
            }
        }
        $this->entityManager->flush();
        $this->entityManager->clear(GoodsOtherTagGoods::class);
    }

    /**
     * 删除聚合标签商品
     * @param GoodsOtherTagGoods $goodsOtherTagGoods
     * @return void
     * @throws \Doctrine\ORM\Exception\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function deleteOtherTagGoods(GoodsOtherTagGoods $goodsOtherTagGoods)
    {
        $this->entityManager->remove($goodsOtherTagGoods);
        $this->entityManager->flush();
    }

    /**
     * 删除聚合标签下的全部商品
     * @param $otherTagId
     * @return void
     */
    public function deleteMoreOtherTagGoods($otherTagId)
    {
        $qb = $this->entityManager->createQueryBuilder();
        $qb->delete(GoodsOtherTagGoods::class, 'g')->where('g.otherTagId = :otherTagId')->setParameter('otherTagId', $otherTagId);
        $qb->getQuery()->execute();
    }
}